//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.qst.crop.controller;

import com.github.pagehelper.PageInfo;
import com.qst.crop.common.Result;
import com.qst.crop.entity.Expert;
import com.qst.crop.entity.Question;
import com.qst.crop.service.ExpertService;
import com.qst.crop.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.json.GsonTester;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;

@Api(
        tags = {"问题模块接口"}
)
@RestController
@RequestMapping({"/question"})
@CrossOrigin
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private ExpertService expertService;

    public QuestionController() {
    }

    @ApiOperation("根据ID查询询问情况")
    @GetMapping({"/selectId/{id}"})
    public Result selectById(@PathVariable("id") Integer id) {
        Question question = this.questionService.selectById(id);
        return new Result(true, 20000, "查询成功", question);
    }

    @ApiOperation("添加询问情报")
    @PostMapping({"/add"})
    public Result add(@RequestBody Question question, BindingResult bindingResult) {
        System.out.println("111asd: " + question + " ||| " + bindingResult);
        if (!bindingResult.hasErrors()) {
            this.questionService.insert(question);
            return new Result(true, 20000, "添加成功");
        } else {
            StringBuffer stringBuffer = new StringBuffer();
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            Iterator var5 = allErrors.iterator();

            while(var5.hasNext()) {
                ObjectError objectError = (ObjectError)var5.next();
                stringBuffer.append(objectError.getDefaultMessage()).append("; ");
            }

            String s = stringBuffer.toString();
            System.out.println(s);
            return new Result(false, 20001, "添加失败", s);
        }
    }

    @ApiOperation("根据id修改询问情报")
    @PutMapping({"/update"})
    public Result update(@RequestBody Question question, BindingResult bindingResult) {
        if (!bindingResult.hasErrors()) {
            this.questionService.updateById(question);
            return new Result(true, 20000, "修改成功");
        } else {
            StringBuffer stringBuffer = new StringBuffer();
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            Iterator var5 = allErrors.iterator();

            while(var5.hasNext()) {
                ObjectError objectError = (ObjectError)var5.next();
                stringBuffer.append(objectError.getDefaultMessage()).append("; ");
            }

            String s = stringBuffer.toString();
            System.out.println(s);
            return new Result(false, 20001, "修改失败", s);
        }
    }

    @ApiOperation("根据id删除询问情报")
    @DeleteMapping({"/delete/{id}"})
    public Result delete(@PathVariable("id") Integer id) {
        this.questionService.delete(id);
        return new Result(true, 20000, "删除成功");
    }

    @ApiOperation("根据用户查询询问情况")
    @GetMapping({"/selectByKind/{kind}"})
    public Result selectByKind(@PathVariable("kind") String kind) {
        List<Question> questions = this.questionService.selectByQuestion(kind);
        System.out.println(questions);
        return new Result(true, 20000, "查询成功", questions);
    }

    @ApiOperation("分页条件查询所有问答情况")
    @GetMapping({"/findPageQues/{keys}/{pageNum}"})
    public Result<PageInfo<Question>> findPageQues(@PathVariable("keys") String keys, @PathVariable Integer pageNum) {
        PageInfo<Question> questionPageInfo = this.questionService.selectByKeys(keys, pageNum);
        return new Result(true, 20000, "查询成功", questionPageInfo);
    }

    @ApiOperation("分页查询所有问答情况")
    @GetMapping({"/findAllQues/{pageNum}"})
    public Result<PageInfo<Question>> findAllQues(@PathVariable Integer pageNum) {
        PageInfo<Question> questionPageInfo = this.questionService.selectByKeys((String)null, pageNum);
        return new Result(true, 20000, "查询成功", questionPageInfo);
    }

    @ApiOperation("分页查询所有专家")
    @GetMapping({"/findExpert/{pageNum}"})
    public Result<PageInfo<Expert>> findPage(@PathVariable Integer pageNum) {
        PageInfo<Expert> expertPageInfo = this.expertService.findPage(pageNum);
        return new Result(true, 20000, "查询成功", expertPageInfo);
    }

    @ApiOperation("分页条件查询专家")
    @GetMapping({"/findExpertByKeys/{keys}/{pageNum}"})
    public Result<PageInfo<Expert>> findExpertByKeys(@PathVariable("keys") String keys, @PathVariable Integer pageNum) {
        PageInfo<Expert> expertPageInfo = this.expertService.findPageByKeys(keys, pageNum);
        return new Result(true, 20000, "查询成功", expertPageInfo);
    }
}
